package com.sihai.ehr.web.action.exam;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.ExamQualStandardSet;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.ExamQualStandSetService;
import com.sihai.ehr.web.action.EHRAction;
import com.sihai.ehr.web.action.invite.InviteFormAction;

/**
 * 定性考核标准控制器
 * 
 * @author Henry
 * 
 */
@Results({
		@Result(name = "exam_qualstand_list", location = "exam_qualstand_list.jsp"),
		@Result(name = "qual_standset_add", location = "exam_qualstand_add.jsp") })
public class ExamQualStandSetAction extends EHRAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8780354538246566516L;
	/** 日志服务 */
	Log log = LogFactory.getLog(InviteFormAction.class);

	@BeanInject
	private ExamQualStandSetService examQualStandSetService;

	// *******************************
	// ************ 页面属性 *************
	// *******************************

	/** 移出的ID集合 */
	private String ids;
	/** 页面标题 */
	private String title;
	/** 定性考核标准设置对象 */
	private ExamQualStandardSet examQualStandardSet;
	/** 考核强制分布集合 */
	private LimitedList<ExamQualStandardSet> examQualStandardSets;

	@Action("/exam/exam_qualstand_list")
	public String queryQualStandSets() throws Exception {
		try {
			examQualStandardSets = examQualStandSetService
					.searchExamQualStandardSets(organizationId, createLimit(),
							createSorter().desc("scoreStart"));
			/* 分页 */
			this.paging(examQualStandardSets);
			/* 设置翻页action */
			this.setPageUrl("exam/exam_qualstand_list.xhtml");
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error("【获取强制分布异常】【错误原因:" + e.getMessage() + "】");
			}
		}
		return "exam_qualstand_list";
	}

	/**
	 * 添加考核强制分布设置请求
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/exam/qual_standset_add")
	public String handleQualStandSet() throws Exception {
		// 检测是否是修改申请
		if (getParameter("i") != null) {
			title = "修改";
			try {
				examQualStandardSet = examQualStandSetService
						.getExamQualStandardSet(Long
								.parseLong(getParameter("i")));
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error("【读取定性考核标识发生异常】【异常原因：" + e.getMessage() + "】");
				}
				return ERROR;
			}
		} else {
			title = "添加 ";
			examQualStandardSet = new ExamQualStandardSet();
			Organization organization = new Organization();
			organization.setId(organizationId);
			examQualStandardSet.setOrganization(organization);
		}
		return "qual_standset_add";
	}

	@Action(value = "/exam/validate_standscore", results = { @Result(name = "success", type = "plainText") })
	public String validateInputScore() throws Exception {
		if (getParameter("st") == null || getParameter("se") == null) {
			return ERROR;
		}
		Long id = -1L;
		if (getParameter("i") != null) {
			id = Long.parseLong(getParameter("i"));
		}
		try {
			boolean isExist = examQualStandSetService.checkScoreAreaExist(
					organizationId,
					Float.parseFloat(getParameter("st").toString()),
					Float.parseFloat(getParameter("se").toString()), id);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.getWriter().write(
					"{\"availability\":" + (isExist ? false : true) + "}");
			response.getWriter().flush();
			response.getWriter().close();
			return SUCCESS;
		} catch (Exception e) {
			return ERROR;
		}

	}

	/** 考核强制分布添加 */
	@Action("/exam/post_qualstandset")
	public String postQualStandSet() throws Exception {

		if (examQualStandardSet == null) {
			if (log.isInfoEnabled()) {
				log.info("【参数异常】");
				return ERROR;
			}
		}
		try {
			if (examQualStandardSet.getId() != null
					&& examQualStandardSet.getId() > 0) {
				examQualStandSetService
						.deleteExamQualStandardSetById(examQualStandardSet
								.getId());
				examQualStandardSet.setId(null);
			}
			// 添加或修改数据
			examQualStandSetService
					.saveExamQualStandardSet(examQualStandardSet);
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error("【保存或修改定性考核标准发生异常】【异常原因：" + e.getMessage() + "】");
			}
			return ERROR;
		}

		return "qual_standset_add";
	}

	/**
	 * 移动考核强制分布设置
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/exam/qual_standset_delete", results = { @Result(name = "success", type = "plainText") })
	public String removeInviteByIds() throws Exception {
		if (ids == null || ids.isEmpty()) {
			if (log.isErrorEnabled()) {
				log.error("【移除定性考核标准分布ID集合参数异常】");
			}
			return ERROR;
		}
		try {
			examQualStandSetService.removeExamQualStandardSets(ids);
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error("【移除定性考核标准发生异常】");
			}
			return ERROR;
		}
		return SUCCESS;
	}

	// ///////////////////

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public ExamQualStandardSet getExamQualStandardSet() {
		return examQualStandardSet;
	}

	public void setExamQualStandardSet(ExamQualStandardSet examQualStandardSet) {
		this.examQualStandardSet = examQualStandardSet;
	}

	public LimitedList<ExamQualStandardSet> getExamQualStandardSets() {
		return examQualStandardSets;
	}

	public void setExamQualStandardSets(
			LimitedList<ExamQualStandardSet> examQualStandardSets) {
		this.examQualStandardSets = examQualStandardSets;
	}

}
